#!/bin/bash

version=v4.88.1

install_packages libfontconfig-dev || exit 1

wget -O /tmp/boxy-svg.zip "https://storage.boxy-svg.com/builds/boxy-svg-${version}-linux-arm64.zip" || exit 1

rm -rf /tmp/boxy-svg
unzip -q /tmp/boxy-svg.zip -d /tmp/boxy-svg || error "Failed to extract!"
rm -f /tmp/boxy-svg.zip
if [ $arch == 64 ];then #to forcibly repackage Boxy SVG on arm64, replace [ $arch == 64 ] with false
  #use precompiled arm64 electron app
  sudo mkdir -p /usr/local/share/icons
  sudo cp /tmp/boxy-svg/*/metadata/com.boxy_svg.BoxySVG.png /usr/local/share/icons || error "Failed to copy app icon"
  sudo cp /tmp/boxy-svg/*/metadata/com.boxy_svg.BoxySVG.svg /usr/local/share/icons || error "Failed to copy app icon"
  
  #move to permanent folder
  sudo rm -rf /opt/boxy-svg
  sudo mv /tmp/boxy-svg/* /opt/boxy-svg || error "Failed to move app files to /opt/boxy-svg!"
  
else
  #repackage electron app for armhf architecture not built by Boxy SVG developer
  "${DIRECTORY}/manage" install-if-not-installed Node.js || error "Failed to install Node.js! This is necessary to compile Boxy SVG."
  
  #install asar and electron-packager
  export NVM_DIR="$HOME/.nvm"
  [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
  
  cd /tmp/boxy-svg/*/resources || exit 1
  rm -rf sourcecode
  npm install -g asar || error "Failed to install asar!"
  asar extract app.asar sourcecode || error "Failed to extract source code with asar!"
  cd sourcecode
  
  electronversion="$(grep electronVersion package.json | sed 's/.*": "//g ; s/",$//g')"
  
  npm install || error "Failed to install npm dependencies!"
  
  #Add stuff to package.json so that build can work (AI-generated)
  sed -i '/^  }$/d ; /^}$/d' package.json
  cat << "EOF" >> package.json
  },
  "scripts": {
    "start": "electron .",
    "dist": "electron-builder"
  },
  "build": {
    "appId": "com.boxy-svg.app",
    "productName": "Boxy SVG",
    "files": [
      "**/*",
      "!**/node_modules/*/{CHANGELOG.md,README.md,README,readme.md,readme}",
      "!dist/**"
    ],
    "directories": {
      "output": "dist"
    },
    "linux": {
      "target": [
        "dir"
      ],
      "category": "Graphics",
      "icon": "icon.png"
    }
  }
}
EOF
  npm install --save-dev electron@${electronversion} || error "Failed to install npm dependency electron!"
  #--ignore-scripts is here to try to fix error on armhf: npm error Error: ENOENT: no such file or directory, copyfile 'vendor/7z-arm.exe' -> 'vendor/7z.exe'
  npm install --save-dev --ignore-scripts electron-builder || error "Failed to install npm dependency electron-builder!"
  
  status "Building Electron app..."
  if [ $arch == 32 ];then
    ./node_modules/.bin/electron-builder --linux --armv7l
  elif [ $arch == 64 ];then
    ./node_modules/.bin/electron-builder --linux --arm64
  else
    error "Failed to detect OS CPU architecture! Something is very wrong."
  fi
  
  status -n "Copying icons... "
  #copy icons
  sudo mkdir -p /usr/local/share/icons
  sudo cp -f ./node_modules/@boxy-svg/boxy-svg/logo.svg /usr/local/share/icons/com.boxy_svg.BoxySVG.svg || error "Failed to copy app icon"
  sudo cp -f ./icon.png /usr/local/share/icons/com.boxy_svg.BoxySVG.png || error "Failed to copy app icon"
  status_green Done
  
  status "Setting up /opt/boxy-svg... "
  #move to permanent folder
  sudo rm -rf /opt/boxy-svg
  sudo mv ./dist/linux-*-unpacked /opt/boxy-svg || error "Failed to move app files to /opt/boxy-svg!"
  status_green Done
fi

cd $HOME
rm -rf /tmp/boxy-svg

#create a terminal command to run Boxy SVG without chrome sandbox for better performance, and use wayland if possible
status "Making terminal command, mime associations, and menu launcher... "
cat << "EOF" | sudo tee /usr/local/bin/boxy-svg >/dev/null
#!/bin/bash
if [ -z "$WAYLAND_DISPLAY" ];then
  /opt/boxy-svg/boxy-svg --no-sandbox "$@"
else
  /opt/boxy-svg/boxy-svg --no-sandbox --enable-features=UseOzonePlatform --ozone-platform=wayland "$@"
fi
EOF
sudo chmod +x /usr/local/bin/boxy-svg

#remove broken mime association
if grep -qF 'image/svg+xml;application/illustrator=boxy-svg.desktop;' ~/.config/mimeapps.list ;then
  echo "$(grep -vF 'image/svg+xml;application/illustrator=boxy-svg.desktop;' ~/.config/mimeapps.list)" > ~/.config/mimeapps.list
fi

if ! grep -qF 'boxy-svg.desktop' ~/.config/mimeapps.list ;then
  echo "Associating the SVG mimetype with Boxy SVG..."
  echo "[Added Associations]
image/svg+xml=boxy-svg.desktop;
application/illustrator=boxy-svg.desktop;" >> ~/.config/mimeapps.list
  
fi

echo "[Desktop Entry]
Name=Boxy SVG
Comment=Scalable Vector Graphics (SVG) editor
Exec=boxy-svg %F
Icon=com.boxy_svg.BoxySVG
Type=Application
Terminal=false
Categories=Graphics
StartupWMClass=Boxy SVG
MimeType=image/svg+xml;image/png;image/jpeg;image/gif;image/webp;application/pdf;application/illustrator;
Keywords=SVG;Vector;Graphics;Editor;" | sudo tee /usr/share/applications/boxy-svg.desktop >/dev/null
status_green Done
